package meihua.stopcarapp.presenter;

import android.util.Log;

import meihua.mylibrary.baserx.RxSubscriber;
import meihua.stopcarapp.R;
import meihua.stopcarapp.bean.UserEntity;
import meihua.stopcarapp.bean.result.UserResult;
import meihua.stopcarapp.presenter.contract.LoginContract;

/**
 * Name: LGZ
 * Time: 2017/5/9.
 * Dec:
 */

public class LoginPresenter extends LoginContract.Presenter {

    @Override
    public void sendCodeRequest(String phone) {
        mRxManage.add(mModel.sendCode(phone)
                .subscribe(new RxSubscriber<String>(mContext,false) {
                    @Override
                    public void onStart() {
                        super.onStart();
                        mView.showLoading(mContext.getString(R.string.loading));
                    }
                    @Override
                    protected void _onNext(String code) {
                        Log.d("ligaozeng","发送验证码");
                        mView.returnPhoneCode(code);
                        mView.stopLoading();
                    }
                    @Override
                    protected void _onError(String message) {
                        Log.d("ligaozeng","服务器错误:"+message);
                    }
                }));
    }

    @Override
    public void loginRequest(String phone, String code) {
        mRxManage.add(mModel.login(phone,code)
                .subscribe(new RxSubscriber<UserResult>(mContext,false) {
                    @Override
                    public void onStart() {
                        super.onStart();
                        mView.showLoading(mContext.getString(R.string.loading));
                    }
                    @Override
                    protected void _onNext(UserResult userEntity) {
                        Log.d("ligaozeng","访问服务器");
                        mView.returnLogin(userEntity);
                        mView.stopLoading();
                    }
                    @Override
                    protected void _onError(String message) {
                        Log.d("ligaozeng","服务器错误:"+message);
                    }
                }));
    }
}
